//Implementation file - Shop.cpp
//private:
/*
WeaponProduct* weaponInventory;
int numWeapons;
ArmorProduct* armorInventory;
int numArmor;

struct WeaponProduct
{
Weapon weapon;
int price;
};

struct ArmorProduct
{
Armor armor;
int price;
};

*/

#include <iostream>
#include <fstream>
#include "Shop.h"

using namespace std;


Shop::Shop(string weaponFile, string armorFile)
{
	ifstream armorInfile;
	ifstream weaponInfile;


	armorInfile.open(armorFile.c_str());
	weaponInfile.open(weaponFile.c_str());

	if (armorInfile)
	{
		armorInfile  >> numArmor;
		armorInventory = new ArmorProduct[numArmor];
	}
	else
		throw -1;

	if (weaponInfile)
	{
		weaponInfile >> numWeapons;
		weaponInventory = new WeaponProduct[numWeapons];
	}
	else
		throw -1.0;


	string tempName;
	int temp;


	for (int i = 0; i < numArmor; i++)
	{

		armorInfile.ignore(10, '\n');
		getline(armorInfile, tempName, '\n');
		armorInventory[i].armor.setName(tempName);

		armorInfile >> temp;
		armorInventory[i].armor.setBonus(temp);
		armorInfile >> temp;
		armorInventory[i].armor.setPenalty(temp);
		armorInfile >> temp;
		armorInventory[i].armor.setMaxDexterity(temp);
		armorInfile >> armorInventory[i].price;



	}

	for (int j = 0; j < numWeapons; j++)
	{
		weaponInfile.ignore(10, '\n');
		getline (weaponInfile, tempName, '\n');
		weaponInventory[j].weapon.setName(tempName);
		weaponInfile >> temp;
		weaponInventory[j].weapon.setMinimumDamage(temp);
		weaponInfile >> temp;
		weaponInventory[j].weapon.setMaximumDamage(temp);
		weaponInfile >> temp;
		weaponInventory[j].weapon.setCriticalMinimum(temp);
		weaponInfile >> temp;
		weaponInventory[j].weapon.setCriticalMultiplier(temp);

		weaponInfile >> weaponInventory[j].price;
	}

	armorInfile.close();
	weaponInfile.close();

}


Shop::~Shop()
{
	delete [] weaponInventory;
	delete [] armorInventory;
}


void Shop::enter(Character& character)
{
	string buy;
	string again;
	int choice;


	/*
	system("cls");
	cout << Flavor text here as they enter the shop;
	*/
	
	cout << "The shopkeep glares at you over his tattered copy of \'Merchant's Monthly\'" << endl;

	cout << "\"So, you gonna buy something?\" (yes/no) >: ";
	
	cin.clear(); 


	cin >> buy;
	cout << endl;

	if (toupper(buy[0]) == 'Y')
	{

		do
		{
			cout << "Do you want to purchase a weapon, buy some armor, or leave the shop?\n(Weapon/Armor/Leave) >: ";
			
			cin.clear(); 
;

			cin >> buy;
			cout << endl;

			while (toupper(buy[0]) != 'W' && toupper(buy[0]) != 'A' && toupper(buy[0]) != 'L')
			{
				cout << "Im sorry, I don't understand that word. \n";
				cout << "Do you want to purchase weapons, armor, or leave the shop?\n(Weapon/Armor/Leave) >: ";
				
				cin.clear(); 


				cin >> buy;
				cout << endl;
			}

			if (toupper(buy[0]) == 'L')
			{
				break;
			}
			if (toupper(buy[0]) == 'W')
			{
				system("cls");
				string yes_no = "Y";

				do
				{
					cout << "Weapon:\t\tPrice:" << endl;

					for (int j = 0; j < numWeapons; j++)
					{
						cout << (j+1) << ". " << weaponInventory[j].weapon.getName();
						cout << "\t\t" << weaponInventory[j].price << " gold" << endl;
					}

					cout << "Which one would you like to look at? >(1-" << numWeapons << "): ";
					
					cin.clear(); 


					cin >> choice;
					cout << endl;
					choice--;

					while (cin.fail())
					{	

                        cin.clear();
                        cin.ignore(100, '\n');

						cout << "Sorry, I use a special numbering system to avoid having another horrible\nmisunderstanding";
						cout << " when someone gets a weapon's name wrong\n(wouldn't want someone else to lose and eye or anything, right?)" << endl;
						cout << "You have to tell me a number between 1-" << numWeapons << " if you want to see a weapon!" << endl << endl;
						
						cout << "Weapon:\t\tPrice:" << endl;

    					for (int j = 0; j < numWeapons; j++)
    					{
    						cout << (j+1) << ". " << weaponInventory[j].weapon.getName();
    						cout << "\t\t" << weaponInventory[j].price << " gold" << endl;
    					}
    
    					cout << "Which one would you like to look at? >(1-" << numWeapons << "): ";
    					
    					cin >> choice;
    					cout << endl;
    					choice--;
					}
					
					if (choice < 0 || choice >= numWeapons)
					{
                        cout << "You have to tell me a number between 1-" << numWeapons << " if you want to see a weapon!" << endl << endl;
                    }
					else
					{
						cout << "Weapon: " << weaponInventory[choice].weapon.getName() << endl;

						cout << "Damage: (" << weaponInventory[choice].weapon.getMinimumDamage() << "-";
						cout << weaponInventory[choice].weapon.getMaximumDamage() << ")"<< endl;

						cout << "Critical Strike Rating: (" << weaponInventory[choice].weapon.getCriticalMinimum();
						cout << ")" << endl;

						cout << "Critical Strike Multiplier: (x" << weaponInventory[choice].weapon.getCriticalMultiplier();
						cout << ")" << endl;

						cout << "Price: " << weaponInventory[choice].price << " gold." << endl;

						if (character.getGold() >= weaponInventory[choice].price)
						{
							cout << "Is this the weapon you want to buy? (Yes/No) >: ";
							
							cin.clear(); 


							cin >> buy;
							cout << endl;

							while (toupper(buy[0]) != 'Y' && toupper(buy[0]) != 'N')
							{
								
								cout << "Look, its a simple yes or no question, do you want to buy this thing or not.\nIm not standing around all day while you window shop!\n";
								cout << "Is this the weapon you want to buy? (Yes/No) >: ";
								
								cin.clear(); 
								cin.ignore(100, '\n');

								cin >> buy;
								cout << endl;
							}

							if (toupper(buy[0]) == 'Y')
							{
								character.setWeapon(weaponInventory[choice].weapon);
								character.setGold(character.getGold() - weaponInventory[choice].price);
								cout << "Congratulations on your shiny new toy, i'm sure that gold would have just\nweighed you down anyway." << endl;
							}
						}
						else
						cout << "The shopkeeper frowns at you in annoyance as he eyes your meager stack of gold. \n\"Hey kid, why are you making me waist my time on this shpeel when\nyou dont even have enough money to buy this thing?\"" << endl << endl;
					}

					cout << "Do you want to look at more weapons? (Yes/No) >: ";
					
					cin.clear(); 


					cin >> yes_no;
					cout << endl;

					while (toupper(yes_no[0]) != 'Y' && toupper(yes_no[0]) != 'N')
					{
						
						cout << "Look, its a simple yes or no question, don't make me come over this counter.\n";
						cout << "Do you want to look at more weapons? (Yes/No) >: ";

						cin.clear(); 
						 

						cin >> yes_no;
						cout << endl;
					}

				}while (toupper(yes_no[0]) == 'Y');
			}


			if (toupper(buy[0]) == 'A')
			{
				system("cls");
				string yes_no = "Y";

				do
				{
					cout << "Armor:\t\tPrice:" << endl;

					for (int i = 0; i < numArmor; i++)
					{
						cout << (i+1) << ". " << armorInventory[i].armor.getName();
						cout << "\t\t" << armorInventory[i].price << " gold" << endl;
					}

					cout << "Which one would you like to look at? >(1-" << numArmor << "): ";
					
					cin.clear(); 
					 

					cin >> choice;
					cout << endl;
					choice--;
                    
                    while (cin.fail())
					{	

                        cin.clear();
                        cin.ignore(100, '\n');
                        
                        cout << "Sorry, I started using a number based tracking system since the\nGreat and Terrible Audit of aught' 12" << endl << endl;
                        cout << "Armor:\t\tPrice:" << endl;

    					for (int i = 0; i < numArmor; i++)
    					{
    						cout << (i+1) << ". " << armorInventory[i].armor.getName();
    						cout << "\t\t" << armorInventory[i].price << " gold" << endl;
    					}
    
    					cout << "Which one would you like to look at? >(1-" << numArmor << "): ";
    					
    					cin >> choice;
    					cout << endl;
    					choice--;
                    }
					if (choice < 0 || choice >= numArmor)
					{
						cout << "You have to tell me a number between 1-" << numArmor << " if you want to see that armor!" << endl << endl;
					}
					else
					{
						cout << "Armor: " << armorInventory[choice].armor.getName() << endl;

						cout << "Armor Bonus: " << armorInventory[choice].armor.getBonus() << endl;


						cout << "Skill Penalty: " << armorInventory[choice].armor.getPenalty() << endl;

						cout << "Maximum Dexterity Bonus: " << armorInventory[choice].armor.getMaxDexterity() << endl;

						cout << "Price: " <<  armorInventory[choice].price << " gold." << endl;

						if (character.getGold() >=  armorInventory[choice].price)
						{
							cout << "Is this the armor you want to buy? (Yes/No) >: ";
							
							cin.clear(); 
							 

							cin >> buy;
							cout << endl;

							while (toupper(buy[0]) != 'Y' && toupper(buy[0]) != 'N')
							{
								cout << "Look, its a simple yes or no question, do you want to buy this thing or not.\nI'm not going to let you put your paws all over my stuff if you aren't!\n";
								cout << "Is this the armor you want to buy? (Yes/No) >: ";
								
								cin >> buy;
								cout << endl;
							}

							if (toupper(buy[0]) == 'Y')
							{
								character.setArmor(armorInventory[choice].armor);
								character.setGold(character.getGold() - armorInventory[choice].price);
								cout << "Congratulations on your shiny new toy, i'm sure that gold would have just\nweighed you down anyway." << endl;
							}
						}
						else
						cout << "The shopkeeper frowns at you in annoyance as he eyes your rather diminuitive\nstack of gold.\n\"Hey kid, why are you making me waist my time on this\nshpeel when you dont even have enough money to buy this?" << endl;
					}

					cout << "Do you want to look at more armor? (Yes/No) >: ";
					
					cin.clear();
					 

					cin >> yes_no;
					cout << endl;

					while (toupper(yes_no[0]) != 'Y' && toupper(yes_no[0]) != 'N')
					{
						cout << "\"Look, its a simple yes or no question, yer' tickin' me off kid.\"\n";
						cout << "Do you want to look at more armor? (Yes/No) >: ";
						
						cin.clear();
						 

						cin >> yes_no;
						cout << endl;
					}

					if (toupper(yes_no[0]) == 'N')
					{
						break;
					}

				}while (toupper(yes_no[0]) == 'Y');

			}

			character.display();

			cout << "\"Alright ya mook, are you going to be spending any money in my establishment?\"\n(Yes/No) >: ";
			
			cin.clear(); 
			 

			cin >> again;
			cout << endl;

			while (toupper(again[0]) != 'Y' && toupper(again[0]) != 'N')
			{
				cout << "\"Look, its a simple yes or no question, are you trying to drive me crazy?\nNo, don't answer that, I dont think I want to know...\"\n";
				cout << "Keep shopping? (Yes/No) >: ";
				
				cin.clear();


				cin >> again;
				cout << endl;
			}
			if (toupper(again[0]) == 'N')
			{
				cout << "You have just enough time to get to the \'n\' in \"no\" before the shop keep pulls\na lever hidden behind the counter and an enormous man emerges from the depths\nof the store.\n\n";
				cout << "As you are being hurled from the shop you can hear the voice of the shopkeeper: \"Thanks for shopping with us, i'd wish you but odds are you'll be dead\ninside the day.\"" << endl;
				system("PAUSE");
			}
		}while (toupper(again[0]) == 'Y');
	}
}
